package com.ddstation.appeal.service;

import com.ddstation.crud.entity.DtInvsIllegalAppealEntity;
import com.ddstation.crud.entity.DtInvsIllegalCheckDailyEntity;
import com.ddstation.crud.mapper.DtInvsIllegalAppealMapper;
import com.ddstation.crud.mapper.DtInvsIllegalCheckDailyMapper;
import com.ddstation.home_page.service.DtInvsMessageQueueService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;

@Service @Lazy
public class AppealServiceImpl implements AppealService {
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int updateAppeal(DtInvsIllegalAppealEntity a){
		//新增消息
		dtInvsMessageQueueService.insertMessageQueue(a.getInstitutionId(),501,2,1,a.getIllegalId(),1);

		return dtInvsIllegalAppealMapper.updateByPrimaryKeySelective(a);
	}
	
	
	// 更新申诉表 + check表
	public int updateAppealAndCheck(DtInvsIllegalAppealEntity a){
		
		// 设置申诉处理状态
		if ( a.getFeedbackSt() ==  0 ){ //同意
			a.setAppealSt(2);//申诉成功
		} else if ( a.getFeedbackSt() ==  -1) {
			a.setAppealSt(-1);//被驳回
		}  else if ( a.getFeedbackSt() ==  10) {
			a.setAppealSt(10);//主动关闭
		} 
		if ( dtInvsIllegalAppealMapper.updateByPrimaryKeySelective(a) != 1){
			return -1;//更新不成功
		};
		
		if ( a.getFeedbackSt() == -1 ){
			return 0;
		}
		// 同意、主动关闭 的情况下，把check的记录更新为正常
		DtInvsIllegalCheckDailyEntity b = new DtInvsIllegalCheckDailyEntity();
		b.setId(a.getIllegalId());
		b.setCheckSt(0);
		if ( dtInvsIllegalCheckDailyMapper.updateByPrimaryKeySelective(b)!=1 ){
			return -2;
		}
		return 0;
	}

	@Override
	public DtInvsIllegalCheckDailyEntity getIllegalDetailById(Integer id) {
		return dtInvsIllegalCheckDailyMapper.selectOne(id);
	}

	@Inject private DtInvsMessageQueueService dtInvsMessageQueueService;
	@Inject private DtInvsIllegalAppealMapper dtInvsIllegalAppealMapper;
	@Inject private DtInvsIllegalCheckDailyMapper dtInvsIllegalCheckDailyMapper;
}
